package com.zwz.controller;

import com.zwz.crawler.annualReports.TungReportsEmail;
import com.zwz.dto.AgentAccountInfoDTO;
import com.zwz.entity.AgentAccountInfo;
import com.zwz.entity.EnterpriseAnnualReport;
import com.zwz.service.AgentAccountInfoService;
import com.zwz.vo.AccountInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/agent-accounts")
public class AgentAccountInfoController {

    @Autowired
    private AgentAccountInfoService service;

    @PostMapping
    public ResponseEntity<?> create(String enterpriseId ) {
        try {
            //1。尝试获取的对应的email 数据

            AccountInfo email = TungReportsEmail.startTungeeAnnualReportQuery(enterpriseId);
            AgentAccountInfo record=new AgentAccountInfo();
            record.setMobile(email.getTel());
            record.setEmail(email.getEmail());
            Integer key=0;
            record.setId(key++);
            service.create(record);

            AgentAccountInfoDTO  dto=new AgentAccountInfoDTO( null,record);



            return ResponseEntity.status(HttpStatus.CREATED).body(dto);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<AgentAccountInfo> getById(@PathVariable Integer id) {
        AgentAccountInfo info = service.getById(id);
        return info != null ? ResponseEntity.ok(info) : ResponseEntity.notFound().build();
    }

    @GetMapping
    public ResponseEntity<List<AgentAccountInfo>> getAll() {
        return ResponseEntity.ok(service.getAll());
    }

    @GetMapping("/hot/{hasHot}")
    public ResponseEntity<List<AgentAccountInfo>> getByHotStatus(@PathVariable Boolean hasHot) {
        return ResponseEntity.ok(service.getByHotStatus(hasHot));
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable Integer id, @RequestBody AgentAccountInfo record) {
        try {
            if (!id.equals(record.getId())) {
                return ResponseEntity.badRequest().body("ID不匹配");
            }
            service.update(record);
            return ResponseEntity.ok(record);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteById(@PathVariable Integer id) {
        service.deleteById(id);
        return ResponseEntity.noContent().build();
    }
}